package com.thinkfit.jy.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.lib.bean.UserBean;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.contract.UpdateProfileContract;
import com.thinkfit.jy.dialog.UserPopupWindow;
import com.thinkfit.jy.presenter.UpdateProfilePresenter;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;

/**
 * 修改用户别名
 */
public class UpdateNickFragment extends BaseFragment<UpdateProfileContract.Presenter> implements UpdateProfileContract.View {
    @NonNull
    @Override
    protected UpdateProfileContract.Presenter onCreatePresenter() {
        return new UpdateProfilePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_update_nick;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        if (DataManager.Companion.getInstance().getMainStyle() == 3) {
            view.setBackground(null);
            setBackground(R.id.llNice,R.drawable.shape_4cffffff_6);
        }
        setViewsOnClickListener(R.id.ivBack,R.id.tv_ok);
        setText(R.id.etNick, DataManager.Companion.getInstance().getMyInfo().getNickname());
        EditText etNick = view.findViewById(R.id.etNick);
        etNick.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                setText(R.id.tvNum, count + "/12");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        etNick.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 软键盘显示
                    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx    软键盘显示");
                    SportsStatusBarViewUtils.getInstance().goneLayout();
                } else {
                    // 软键盘隐藏
                    System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx    软键盘隐藏");
                    SportsStatusBarViewUtils.getInstance().visibleLayout();
                }
            }
        });
        etNick.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx    keyCode : "+keyCode);
                if(keyCode == KeyEvent.KEYCODE_ENTER){
//                    SportsStatusBarViewUtils.getInstance().visibleLayout();
                    // 获取InputMethodManager
                    InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    // 隐藏键盘
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                    etNick.clearFocus();
                }
                return false;
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.ivBack:
            case R.id.tv_ok:
                String nick = getTextById(R.id.etNick);
                UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
                if (TextUtils.isEmpty(nick) || myInfo.getNickname().equals(nick)) {
                    finish();
                    return;
                }
                UserPopupWindow.OnUserOlick onUserOlick = new UserPopupWindow.OnUserOlick() {
                    @Override
                    public void onClick() {
                        getPresenter().updateUser("nickname", nick);
                    }
                };
                UserPopupWindow userPopupWindow = new UserPopupWindow(getContext(), onUserOlick);
                userPopupWindow.show(getActivity());

                break;
        }
    }

    @Override
    public void updateUserSuccess(Object nick) {
        if (getView() == null) {
            return;
        }
        UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
        myInfo.setNickname((String) nick);
        DataManager.Companion.getInstance().saveMyInfo(myInfo);
        finish();
    }
}
